//    Copyright (C) Kherty.  All rights reserved.
using System.Collections.Generic;
using System.Collections.ObjectModel;
using OpenLS.Core.Serialization;

namespace OpenLS.Chart
{
    public class RadarChart : ChartBase
    {
        private ObservableCollection<RadarSeries> _series;
        private DataLabels dlbls;
        private Axis firstAxis;


        private RadarStyle radarStyle;
        private Axis secondAxis;

        private bool varyColors;

        public ObservableCollection<RadarSeries> Series
        {
            get
            {
                if (_series == null)
                    _series = CreateSeriesCollection<RadarSeries>();
                return _series;
            }
        }

        public RadarStyle RadarStyle
        {
            get { return radarStyle; }
            set { radarStyle = value; notifyPropertyChanged("RadarStyle");}
        }

        public bool VaryColors
        {
            get { return varyColors; }
            set
            {
                varyColors = value;
                notifyPropertyChanged("VaryColors");
            }
        }

        public Axis FirstAxis
        {
            get { return firstAxis; }
            set { firstAxis = value; }
        }

        public Axis SecondAxis
        {
            get { return secondAxis; }
            set { secondAxis = value; }
        }

        public DataLabels DataLabels
        {
            get { return dlbls; }
            set { dlbls = value; notifyPropertyChanged("DataLabels");}
        }


        protected override void VerifyAxes()
        {
            this.firstAxis.CrossAxis = SecondAxis;
            secondAxis.CrossAxis = firstAxis;
            
        }

        internal override void ReadXmlCore(ReadContext context)
        {
            using (var c = context.Read("radarChart"))
            {
                RadarStyle = c.ReadElementEnumAttribute<RadarStyle>("radarStyle");
                VaryColors = ChartHelper.ReadVaryColors(c);
                Series.Clear();
                while (c.ReaderLocalName == "ser")
                {
                    var s = new RadarSeries();
                    s.ReadXmlCore(c);
                    Series.Add(s);
                }
                DataLabels = c.ReadOptionalElement<DataLabels>(DataLabels.xmlName);
                AxisResolver r = ReadContextData.GetAxisResolver(c);
                r.AddAction(c.ReadElementUIntegerAttribute("axId"), delegate(Axis a) { FirstAxis = a; });
                r.AddAction(c.ReadElementUIntegerAttribute("axId"), delegate(Axis a) { SecondAxis = a; });
            }
        }

        internal override void WriteXmlCore(WriteContext context)
        {
            using (WriteContext c = context.Write("radarChart"))
            {
                c.WriteElementEnumAttribute("radarStyle", RadarStyle);
                ChartHelper.WriteVaryColors(c, VaryColors);
                foreach (RadarSeries s in Series)
                    c.WriteElement(s);
                c.WriteOptionalElement(DataLabels);
                c.WriteElementUIntegerAttribute("axId", FirstAxis.Id);
                c.WriteElementUIntegerAttribute("axId", SecondAxis.Id);
            }
        }

        internal override IEnumerable<Axis> GetAxes()
        {
            yield return FirstAxis;
            yield return SecondAxis;
        }
    }
}